//
//  ConsumerListHeadView.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/1/22.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "ConsumerListHeadView.h"

@interface ConsumerListHeadView () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet ConsumerListHeadButton *timeButton;
@property (weak, nonatomic) IBOutlet ConsumerListHeadButton *projectButton;
@property (weak, nonatomic) IBOutlet ConsumerListHeadButton *ageButton;
@property (weak, nonatomic) IBOutlet ConsumerListHeadButton *loctionButton;
@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;

@end

@implementation ConsumerListHeadView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.autoresizingMask = UIViewAutoresizingNone;
    

    
    self.searchTextField.delegate = self;
    self.searchTextField.backgroundColor = kClearColor;
    
    self.searchView.layer.cornerRadius = 6.0f;
    self.searchView.clipsToBounds = YES;

    
//    self.searchButton.enabled = NO;
//    [self.searchButton addTarget:self action:@selector(searchBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    [self.loctionButton hiddeImage];
    
    self.timeButton.tag = ConsumerListHeadButtonTypeTime;
    self.projectButton.tag = ConsumerListHeadButtonTypeProject;
    self.ageButton.tag = ConsumerListHeadButtonTypeAge;
    self.loctionButton.tag = ConsumerListHeadButtonTypeLoction;
    
    NSArray *btns = @[self.timeButton, self.projectButton, self.ageButton, self.loctionButton];
    for (ConsumerListHeadButton *btn in btns) {
        [btn addTarget:self action:@selector(filterBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}


- (void)setButtonTitle:(NSString *)title buttonType:(ConsumerListHeadButtonType)type isReset:(BOOL)isReset {
    switch (type) {
        case ConsumerListHeadButtonTypeTime: {
            if (isReset) {
                [self.timeButton setTitle:@"时间" forState:UIControlStateNormal];
            }else {
                [self.timeButton setTitle:title forState:UIControlStateNormal];
            }
        }
            break;
            
        case ConsumerListHeadButtonTypeProject: {
            if (isReset) {
                [self.projectButton setTitle:@"项目" forState:UIControlStateNormal];
            }else {
                [self.projectButton setTitle:title forState:UIControlStateNormal];
            }
        }
            break;
            
        case ConsumerListHeadButtonTypeAge: {
            if (isReset) {
                [self.ageButton setTitle:@"年龄段" forState:UIControlStateNormal];
            }else {
                [self.ageButton setTitle:title forState:UIControlStateNormal];
            }
        }
            break;
            
        case ConsumerListHeadButtonTypeLoction: {
            if (isReset) {
                [self.loctionButton setTitle:@"所在地" forState:UIControlStateNormal];
            }else {
                [self.loctionButton setTitle:title forState:UIControlStateNormal];
            }
        }
            break;
    }
}


- (void)resignFirstResponder {
    [self.searchTextField resignFirstResponder];
}



#pragma mark - UITextFieldDelegate

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(consumerListHeadView:didStartSearchKey:)]) {
        [self.delegate consumerListHeadView:self didStartSearchKey:textField.text];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(consumerListHeadView:didStartSearchKey:)]) {
        [self.delegate consumerListHeadView:self didStartSearchKey:textField.text];
    }
    return YES;
}

#pragma mark - Action
- (void)filterBtnDidClick:(ConsumerListHeadButton *)sender {
    ConsumerListHeadButtonType type = sender.tag;
    
    if ([self.delegate respondsToSelector:@selector(consumerListHeadView:didHeadButtonType:)]) {
        [self.delegate consumerListHeadView:self didHeadButtonType:type];
    }
}

+ (CGFloat)viewHeight {
    return 44.f * 2;
}

@end





///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation ConsumerListHeadButton

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupUI];
}

- (void)setupUI {
    self.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    self.titleLabel.textColor = kRGBColor(153, 153, 153);
    
    [self updateEdgeInsets];
}

//- (void)hiddeImage {
//    [self setImage:nil forState:UIControlStateNormal];
//    [self setImage:nil forState:UIControlStateSelected];
//}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    [super setTitle:title forState:state];
    
    [self updateEdgeInsets];
}
 

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageView.app_width = 12;
    self.imageView.app_height = self.imageView.app_width;
    
    self.imageView.contentMode = UIViewContentModeCenter;
}

- (void)updateEdgeInsets {
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.imageView.image.size.width, 0, self.imageView.image.size.width)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(-3, self.titleLabel.bounds.size.width+6, 0, -self.titleLabel.bounds.size.width)];
}

@end
